<template>
  <base-breadcrumb
    :title="breadcrumb"
    :breadcrumb="!isAudit"
  >
    <a-spin :spinning="getNodeTreeLoading">
      <lastMonthPlan ref="lastMonthPlan" :isAudit="isAudit" businessType="0" :allData="allData0" :rootId="rootId"></lastMonthPlan>
      <thisMonthPlan ref="thisMonthPlan" :isAudit="isAudit" businessType="1" :allData="allData1" :rootId="rootId"></thisMonthPlan>
      <nextMonthPlan ref="nextMonthPlan" :isAudit="isAudit" businessType="2" :allData="allData2" :rootId="rootId"></nextMonthPlan>
    </a-spin>
    <div
      v-if="!isConsultation"
      class="page-btn-right-top"
    >
      <a-button
        v-if="!isDetail && !isAudit"
        type="primary"
        :loading="submiting"
        @click="handleSubmit(0)"
        style="margin-right: 10px"
      >
        保存草稿
      </a-button>
      <a-button
        v-if="!isDetail && !isAudit"
        type="primary"
        :loading="submiting"
        @click="handleSubmit(1)"
        style="margin-right: 10px"
      >
        提交
      </a-button>
      <a-button @click="$close">
        关闭
      </a-button>
    </div>
     <!-- 选择审批人 -->
      <selectApprover
         :businessId="businessId"
         :processConfig="setProcessConfig"
       />
       <!-- 选择审批人 -->

  </base-breadcrumb>
</template>
<script>
import lastMonthPlan from './components/monthPlanDetail'
import thisMonthPlan from './components/monthPlanDetail'
import nextMonthPlan from './components/monthPlanDetail'
import monthPlanApi from '@/api/construction/monthPlanApi'
export default {
  name: 'monthPlanView' + Date.now(),
  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    }
  },
  components: {
    lastMonthPlan,
    thisMonthPlan,
    nextMonthPlan
  },
  data() {
    return {
      getNodeTreeLoading: false,
      allData0:  {},
      allData1:  {},
      allData2:  {},
      rootId: '',
      // 面包屑名字
      breadcrumb: '',
      // 提交
      submiting: false,
      // 表格加载
      loading: false,
      // 流程配置
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId: null,
    }
  },
  computed: {
    // 是查看
    isDetail() {
      return this.$route.query.pageType === 'detail' || this.isAudit
    }
  },
  async mounted() {
    this.setBreadcrumb()
    this.init()
    this.processConfig = await this.$getConfigFn('month_plan')
  },
  methods: {
    init() {
      let rootId = ''
      if (this.isAudit) {
        rootId = this.$route.query.businessId
      } else {
        rootId = this.$route.query.rootId
      }
      this.rootId = rootId
      if (rootId) {
        // businessType 0 上月 1 本月 2 下月
        // lookType 1 平铺视图 0 树状视图
        const lookType = this.isDetail ? 1 : 0
        this.getNodeTreeLoading = true
        monthPlanApi.getNodeTree({rootId, lookType }).then(res => {
          if (res.data) {
            const { data } = res
            if (data['0']) {
              this.allData0 = data['0']
            }
            if (data['1']) {
              this.allData1 = data['1']
            }
            if (data['2']) {
              this.allData2 = data['2']
            }
          }
        }).finally(() => {
          this.getNodeTreeLoading = false
        })
      }
    },
    /**
     * @description: 设置面包屑名字
     * @param {void}
     * @return {void}
     */
    setBreadcrumb() {
      const text = {
        detail: '查看',
        update: '编辑'
      }
      this.breadcrumb = `${text[this.$route.query.pageType]}周计划`
    },
    /**
     * @description: 提交
     * @param {string} auditStatus 操作类型
     * @return {void}
     */
    handleSubmit(auditStatus) {
      this.setProcessConfig = null
      this.submitApprovalConfirm(auditStatus === 1 ? this.processConfig : 6, (flag) => {
        if (!flag) return
        this.submiting = true
        monthPlanApi
          .submitAuditStatus(this.rootId, auditStatus)
          .then((res) => {
            if (res.code === 200 || res.data?.code === 200) {
              this.$message.success('操作成功！')
              // 提交成功后关闭页面
              if (auditStatus === 1) {
                this.setProcessConfig = this.processConfig
                this.businessId = res.data?.id
                //this.$close()
              }
            }
          })
          .catch((err) => {
            console.log(err)
          })
          .finally(() => {
            this.submiting = false
          })
      })
    },
  }
}
</script>
<style lang="less">
.row-active {
  background-color: #e6f7ff;
}
</style>
<style lang="less" scoped>

</style>
